package it.csi.finregio.finreges.client.validators;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.EmailValidator;
import org.apache.commons.validator.UrlValidator;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import it.crp.crparch.web.struts.validator.CRPStrutsValidator;
import it.csi.finregio.finreges.presentation.form.SchedaForm;

public class InserisciInfoAssEnteValidator implements CRPStrutsValidator {

	public ActionMessages validate(ActionMapping mapping,
			HttpServletRequest request, ActionForm form) {
		SchedaForm sf = (SchedaForm) form;
		String email1 = sf.getEmail1();
		String email2 = sf.getEmail2();
		String email3 = sf.getEmail3();
		String sito1 = sf.getSito1();
		String sito2 = sf.getSito2();
		String sito3 = sf.getSito3();
		ActionMessages am = new ActionMessages();
		String keyMsg = "alert.email.errata";
		int mode = Integer.parseInt(sf.getModalitaInserimento());
		String idAssessorato = null;
		switch (mode) {
		case SchedaForm.STANDARD:
			idAssessorato = request.getParameter("idAssessorato");
			break;
		case SchedaForm.NON_STANDARD:
			idAssessorato = request.getParameter("idAssessoratoLibero");
			break;
		case SchedaForm.ALTRI_ENTI:
			idAssessorato = request.getParameter("altroEnte");
			break;
		}
		if (idAssessorato.equals("-1")) {
			am.add("idAssessorato", new ActionMessage("alert.field.mandatory",
					new Object[] { "assessorato" }));
		}
		if (am.isEmpty()) {
			EmailValidator emailValidator = EmailValidator.getInstance();
			if (email1 != null && !email1.trim().equals("")
					&& !emailValidator.isValid(email1)) {
				am.add("email1", new ActionMessage(keyMsg,
						new Object[] { "email1" }));
			}
			if (email2 != null && !email2.trim().equals("")
					&& !emailValidator.isValid(email2)) {
				am.add("email2", new ActionMessage(keyMsg,
						new Object[] { "email2" }));
			}
			if (email3 != null && !email3.trim().equals("")
					&& !emailValidator.isValid(email3)) {
				am.add("email3", new ActionMessage(keyMsg,
						new Object[] { "email3" }));
			}
			keyMsg = "alert.sito.errata";
			if (sito1 != null && !sito1.trim().equals("")) {
				if (!urlIsValid(sito1)) {
					am.add("sito1", new ActionMessage(keyMsg,
							new Object[] { "sito1" }));
				}
			}
			if (sito2 != null && !sito2.trim().equals("")) {
				if (!urlIsValid(sito2)) {
					am.add("sito2", new ActionMessage(keyMsg,
							new Object[] { "sito2" }));
				}
			}
			if (sito3 != null && !sito3.trim().equals("")) {
				if (!urlIsValid(sito3)) {
					am.add("sito3", new ActionMessage(keyMsg,
							new Object[] { "sito3" }));
				}
			}
		}
		return am;
	}

	private boolean urlIsValid(String url) {

		try {
			return url.substring(0, "http://".length()).equals("http://");
		} catch (Exception e) {
			return false;
		}
	}

}
